package org.soarsky.wealthchengdu.module.home;

import java.util.ArrayList;
import java.util.List;

import org.soarsky.wealthchengdu.R;
import org.soarsky.wealthchengdu.appliction.MyAppliction;
import org.soarsky.wealthchengdu.module.info.InfomationActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 主界面九宫格图片适配器
 * 
 * @author Administrator
 * 
 */
public class GridView_adapter extends BaseAdapter {
	private boolean chooseflag = true;
	public Context context;
	public List<HomeBean> list = new ArrayList<HomeBean>();
	public HolderView holderview = null;

	public GridView_adapter(Context context) {
		this.context = context;
	}

	public void setList(List<HomeBean> list) {
		this.list = list;
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			holderview = new HolderView();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.home_gridview, null);
			holderview.imageV = (ImageView) convertView
					.findViewById(R.id.home_grid_images);
			holderview.grid_name = (TextView) convertView
					.findViewById(R.id.home_grid_name);
			holderview.grid_context = (TextView) convertView
					.findViewById(R.id.home_grid_content);
			convertView.setTag(holderview);
		} else {
			holderview = (HolderView) convertView.getTag();
		}
		if (MyAppliction.mChoose == position) {
			holderview.imageV.setImageResource(list.get(position).imagedown);
		} else {
			holderview.imageV.setImageResource(list.get(position).image);
		}

		holderview.imageV.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (chooseflag) {
					MyAppliction.mChoose = position;
					notifyDataSetChanged();
					Intent intent = new Intent(context,
							InfomationActivity.class);
					Bundle bundle = new Bundle();
					bundle.putInt("data", position);
					intent.putExtras(bundle);
					context.startActivity(intent);
				}
//				chooseflag = false;
			}
		});
		holderview.grid_name.setText(list.get(position).name);
		holderview.grid_context.setText(list.get(position).details);
		return convertView;
	}

	/**
	 * adapter的优化
	 * 
	 * @author Administrator
	 * 
	 */
	class HolderView {
		ImageView imageV;
		TextView grid_name;
		TextView grid_context;
	}
}
